package integracion.Motor;

import java.io.*;

import negocio.Motor.TransferMotor;

public class DAOMotorImp implements DAOMotor{
	private  int lastId;
	

	public DAOMotorImp() 
	{ 	  
	  try
	  {
		this.lastId=-1;
		File archivo=new File("C:/cardealer/Motor/index.txt");
		if(archivo.exists())
		 	{
			 BufferedReader bfr = new BufferedReader(new FileReader(archivo));
			 this.lastId=Integer.parseInt(bfr.readLine());		
			 bfr.close();
		 	}
		 else
		 	{
			File directorio = new File("c:/cardealer/Motor/");
			directorio.mkdirs();			
			PrintWriter pw = new PrintWriter( new FileWriter((archivo)));			
			pw.println(this.lastId);
			pw.close();
		 	}			
	  }catch(IOException e)
	  {
		 e.printStackTrace();
	  }
	}
	
	public boolean create(TransferMotor transfer)
	{		
		int id = idForParams(transfer.getCilindrada() + " " + transfer.getTipoGasolina());			
		Boolean resultado=false;
		try
		{
		if(id>-1)
			{
			 File archivo= new File("c:/cardealer/Motor/" + Integer.toString(id) + ".txt");				
			 PrintWriter pw = new PrintWriter( new FileWriter((archivo)),true);	
			 pw.println("true");
			 pw.println(transfer.getCilindrada());
			 pw.println(transfer.getTipoGasolina());
			 pw.close();				
			resultado = true;
			}
		else
			{
			 lastId++;
			 File archivo= new File("c:/cardealer/Motor/" + Integer.toString(lastId) + ".txt");
			 File index = new File ("c:/cardealer/Motor/index.txt");
		
    		 PrintWriter pw = new PrintWriter( new FileWriter((archivo)));
			 PrintWriter pwindex = new PrintWriter( new FileWriter((index)));
			
			 pw.println("true");
			 pw.println(transfer.getCilindrada());
			 pw.println(transfer.getTipoGasolina());
			 pwindex.println(lastId);
			 pw.close();
			 pwindex.close();			
			 resultado = true;
			}
		}
		catch(IOException e)
		{
		 e.printStackTrace();
		}
		return resultado;
	}				

	public boolean delete(TransferMotor transfer){	
		File archivo = new File("c:/cardealer/Motor/"+transfer.getId()+".txt");
		try
		{				
		 BufferedReader bfr = new BufferedReader(new FileReader(archivo));
		 bfr.readLine();
		 String cilindrada = bfr.readLine();
		 String gasolina = bfr.readLine();
		 PrintWriter pw = new PrintWriter( new FileWriter((archivo)));	
		 pw.println("false");
		 pw.println(cilindrada);
		 pw.println(gasolina);
		 bfr.close();
		 pw.close();
		 return true;		 
		}catch(Exception e){
			e.printStackTrace();
		 }
	   return false; 
	}

	public boolean update(TransferMotor transfer)  {
		
	File archivo=new File("C:/cardealer/Motor/"+transfer.getId()+".txt");				
		try
		  {			
			PrintWriter pw = new PrintWriter( new FileWriter((archivo)));					
			pw.println("true");
			pw.println(transfer.getCilindrada());
			pw.println(transfer.getTipoGasolina());
			pw.close();	
			return true;								
		  }catch(Exception e){
		    	e.printStackTrace();
		     }	
		return false;
	}
		
	public TransferMotor readById(int id) {
		
		File archivo= new File("c:/cardealer/Motor/" + Integer.toString(id) + ".txt");
		TransferMotor transfer=null;		
		try
		{		
		 BufferedReader bfr = new BufferedReader(new FileReader(archivo));	
		 bfr.readLine();
		 transfer = new TransferMotor(id,bfr.readLine(),bfr.readLine());			 
		 bfr.close();
		}catch(IOException e)
		{
			e.printStackTrace();
		}
		return transfer;
	}	
	
	public boolean creable(TransferMotor transfer)
	{
	 int id = idForParams(transfer.getCilindrada() + " " + transfer.getTipoGasolina());
	 if (id<0) return true;
	 if(!checkFlag(id)) return true;
	 return false;
	}
	
	public boolean deletable(TransferMotor transfer)
	{
	 if(transfer.getId()<0 || transfer.getId()>lastId) return false;
	 if(!checkFlag(transfer.getId()))return false;
	 return true;
	}
	
	public boolean updatable(TransferMotor transfer)
	{
		if(transfer.getId()<0 || transfer.getId()>lastId) return false;
		if(checkFlag(transfer.getId())) return true;
		return false;
	}
	
	public boolean searchable(int id)
	{
	 if(id<0 || id>lastId)return false;
	 if(!checkFlag(id)) return false;
	 return true;
	}
	
	private int idForParams(String params) 
	{	
	 try
	   {	
	  for (int i=0;i<=this.lastId;i++)
		 {
		   File archivo= new File("c:/cardealer/Motor/" + Integer.toString(i) + ".txt");
		   BufferedReader bfr = new BufferedReader(new FileReader(archivo));
		   bfr.readLine();
		   if(params.equalsIgnoreCase(bfr.readLine()+" "+bfr.readLine()))
			   return i;
		   }
		}catch(IOException e)
		    {
		     e.printStackTrace();
		    }	  	
	 return -1;
	}
	
	private boolean checkFlag (int id)
	{		
	   File archivo= new File("c:/cardealer/Motor/" + Integer.toString(id) + ".txt");
		{
		  try{
			  BufferedReader bfr = new BufferedReader(new FileReader(archivo));	 
			  return Boolean.parseBoolean(bfr.readLine());
		  	 }catch(Exception e)
		  	 {e.printStackTrace();			
		  	 }
		 } 	 
		return false;
	}
	
}